
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>专业管理</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/lib/bootstrap/bootstrap.min.css" media="all">
  </head>
  <body style="font-size: 0.9rem;" class="m-3">
    <%--    搜索栏--%>
    <div class="d-flex">
      <div class="col-auto">
        <input id="majorName" class="form-control form-control-sm" value="" placeholder="请输入专业名称"/>
      </div>
      <button id="searchBtn" class="layui-btn layui-btn-sm layui-btn-primary">
        <i class="layui-icon layui-icon-search"></i>搜索
      </button>
      <button id="resetBtn" style="border: 1px solid #FF7670;color: #FF7670;"
              class="layui-btn layui-btn-sm layui-btn-primary"><i
        class="layui-icon layui-icon-close">
      </i>重置
      </button>
      <button id="addBtn" class="layui-btn layui-btn-sm layui-btn-normal"><i
        class="layui-icon layui-icon-addition"></i>新增
      </button>
    </div>
    <%--      定义表格渲染的容器--%>
    <table id="majorTable" lay-filter="majorTable"></table>
    <%--      表格头部工具条--%>
    <script id="toolbar" type="text/html">
    
    </script>
    <%--      行工具条--%>
    <script type="text/html" id="linebar">
      <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑
      </a>
      <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除
      </a>
    </script>
    <script src="${pageContext.request.contextPath}/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/js/lay-config.js" charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/js/http.js" charset="utf-8"></script>
    <script>
        //加载表格模块
        layui.use(['table', 'layer'], function () {
            var table = layui.table;
            var layer = layui.layer;
            //渲染表格
            table.render({
                elem: '#majorTable',//指定表格挂载的容器
                height: 'full-80',
                url: '/major.do?method=lsit',//请求数据接口地址
                toolbar: '#toolbar',
                cols: [[
                    {field: 'majorName', title: '专业名称'},
                    {field: 'orderNum', title: '专业序号'},
                    {width: 178, align: 'center', toolbar: '#linebar'}
                ]],
                page: true,
                limit: 10,
                limits: [10, 20, 30, 50]
            })
            //搜索按钮点击事件
            $("#searchBtn").click(function () {
                reload();
            })
            //重置按钮点击事件
            $("#resetBtn").click(function () {
                //清空搜索框的值
                $("#majorName").val('')
                reload()
            })

            function reload() {
                // alert('进入')
                table.reload('majorTable', {
                    where: { //设定异步数据接口的额外参数，任意设
                        majorName: $("#majorName").val()
                    },
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            }

            //行工具条的点击事件
            table.on('tool(majorTable)', function (obj) {
                console.log(obj)
                if (obj.event == 'edit') { //编辑
                    var index = layer.open({
                        title: '编辑专业',
                        type: 2, //iframe的形式
                        offset: '0px',
                        skin: "layui-layer-molv",//设置皮肤
                        area: ['650px', '350px'],
                        content: '/major.do?method=editUI&majorId=' + obj.data.majorId,
                        btn: ['确定', '取消'],
                        btn1: function (index) {
                            var parms = window['layui-layer-iframe' + index].addData();
                            if (parms) {
                                //发送请求给后端接口
                                http.post("/major.do?method=editSave", parms, function (result) {
                                    if (result.code == 200) {
                                        layer.msg(result.msg);
                                        layer.close(index)
                                        //刷新表格
                                        reload()
                                    }
                                })
                            }
                        },
                        btn2: function (index) {

                        }
                    })
                }
                if (obj.event == 'del') {
                    layer.confirm('确定删除该数据吗?', {icon: 3, title: '系统提示'}, function (index) {
                        //do something
                        http.post("/major.do?method=delete", {majorId: obj.data.majorId}, function (result) {
                            if (result.code == 200) {
                                layer.msg(result.msg);
                                layer.close(index)
                                //刷新表格
                                reload()
                            }
                        })
                        // layer.close(index);
                    });
                }
            })
            //新增按钮点击事件
            $("#addBtn").click(function () {
                var index = layer.open({
                    title: '新增专业',
                    type: 2, //iframe的形式
                    offset: '0px',
                    skin: "layui-layer-molv",//设置皮肤
                    area: ['650px', '350px'],
                    content: '/major.do?method=addUI',
                    btn: ['确定', '取消'],
                    btn1: function (index) {
                        var parms = window['layui-layer-iframe' + index].addData();
                        if (parms) {
                            //发送请求给后端接口
                            http.post("/major.do?method=addSave", parms, function (result) {
                                if (result.code == 200) {
                                    layer.msg(result.msg);
                                    layer.close(index)
                                    //刷新表格
                                    reload()
                                }
                            })
                        }
                    },
                    btn2: function (index) {

                    }
                })
            })
        })
    </script>
  </body>
</html>
